package controllers.common.filter;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import play.Logger;
import play.Play;
import play.mvc.Before;
import play.mvc.Controller;
import utils.json.JSONUtils;

import com.ning.http.util.Base64;

import component.UserBis;
import constants.Constants;

/**
 * 基础拦截器，判定登陆和权限
 * 
 * 2015年9月22日 上午9:50:29
 * 
 * @auth huan208050@vip.qq.com
 */
public class BaseFilter extends Controller {
	/**
	 * 基础拦截器
	 * 
	 * 2015年9月17日 上午9:14:44
	 * 
	 * @auth huan208050@vip.qq.com
	 */
	@Before(unless = { "login.LoginAction.login", "login.LoginAction.loginDo",
			"Application.demo", "login.LoginAction.singleLanding",
			"login.LoginAction.error","login.LoginAction.getMenus","terminal.TermialInventoryAction.getOtherTerminalCutterInventoryByCustomerId" })
	/**
	 * 1.检查登陆状态
	 * 2.判定客户权限
	 * 2015年9月17日 下午8:12:07
	 * @auth huan208050@vip.qq.com
	 */
	public static void inject() {
	 
		String type = UserBis.currType();
		String username = UserBis.currUserName();
		String customer_id = UserBis.currUser();
		String ready_member_id = UserBis.currReadyMemberId();
		String login_account = UserBis.currLoginAccount();
		Map<String,String> cmap = UserBis.currRightsMap();
		String msg = "";	
		String operator_Id = UserBis.currOperatorId();
		String plat_id = UserBis.currPlatId();
		if(Constants.IS_AUTH.equals("2")){
			UserBis.setCurrUser("MS_C0000015", "丰彩科技", "1",
					"2", "2","1","test");
			String menus = JSONUtils.getMenuJson(plat_id);
			UserBis.setCurrMenu(menus);
			UserBis.setCurrRightMap(cmap);
		} else{
		if (StringUtils.isBlank(customer_id) || "null".equals(customer_id)) {
			UserBis.loginOut();
			StringBuffer sb = new StringBuffer();
			msg = "登录超时";
			String url = Play.configuration.getProperty("login_url");
			msg = Base64.encode(msg.getBytes());
			sb.append(url).append("?").append("msg=").append(msg);
			redirect(sb.toString());
		} else {
			renderArgs.put("type", type + "");
			if (StringUtils.isNotBlank(username)) {
				renderArgs.put("username", username + "");
			}
			/*Logger.info("操作平台ID：" + UserBis.currUser() + ",操作用户id："
					+ operator_Id);*/
//			Logger.info("权限列表打印" + UserBis.currRights());

			if (StringUtils.isNotBlank(UserBis.currRights())) {
				String menus = UserBis.currMenu();
				if (StringUtils.isBlank(menus) || menus.equals("null")) {
					menus = JSONUtils.getMenuJson(plat_id);
					if(StringUtils.isBlank(menus)){
						UserBis.loginOut();
						StringBuffer sb = new StringBuffer();
						msg = "访问受限";
						String url = Play.configuration.getProperty("login_url");
						Logger.error("访问受限："+url);
						msg = Base64.encode(msg.getBytes());
						sb.append(url).append("?").append("msg=").append(msg);
						redirect(sb.toString());
					}
					UserBis.setCurrMenu(menus);
				}
				String requestUrl = request.path.toLowerCase();
				//String menusToLower = UserBis.currRights().toLowerCase();
				
				//if (!menusToLower.contains(requestUrl)) {
				if (!cmap.containsKey(requestUrl)) {
					UserBis.loginOut();
					StringBuffer sb = new StringBuffer();
					msg = "访问受限";
					String url = Play.configuration.getProperty("login_url");
					Logger.error("访问受限："+url);
					msg = Base64.encode(msg.getBytes());
					sb.append(url).append("?").append("msg=").append(msg);
					redirect(sb.toString());
				}
			} else {
				UserBis.loginOut();
				StringBuffer sb = new StringBuffer();
				msg = "访问受限";
				String url = Play.configuration.getProperty("login_url");
				msg = Base64.encode(msg.getBytes());
				sb.append(url).append("?").append("msg=").append(msg);
				redirect(sb.toString());
			}
		}
		/**
		 * 有权限，放行
		 */
		
 		UserBis.setCurrUser(customer_id, username, operator_Id,
				plat_id, type,ready_member_id,login_account);
		UserBis.setCurrRightMap(cmap);
	}
	}	
	public static void main(String[] args) {
		String source = "/customer_plat/home.homeaction/getRightsByCondition";
		String p = "/customer_plat/home.homeaction/getRights";
		System.out.println(source.contains(p));
	}
}
